package com.geekchique.runningbuddy.util;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import com.geekchique.runningbuddy.model.Jog;
import com.geekchique.runningbuddy.provider.JogsDBSQL;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class DBHelperImpl implements IDBHelper{

	private SQLiteDatabase db;
	private static final String TAG = "DBHelper";
	
	DBHelperImpl(){
		
	}

    public DBHelperImpl(Context ctx) {
    	
        try {
        	db = ctx.openDatabase(JogsDBSQL.DATABASE_NAME, null);
        } catch (FileNotFoundException e) {
            createJoggingEntriesDB(ctx);
        }
    }

	public void createJoggingEntriesDB(Context ctx) {
		try {
		    db = ctx.createDatabase(JogsDBSQL.DATABASE_NAME, JogsDBSQL.DATABASE_VERSION, 0, null);
		    db.execSQL(JogsDBSQL.DATABASE_CREATE);
		    Log.i(TAG, "Created new JoggingEntries DB");
		} catch (FileNotFoundException e1) {
		    db = null;
		    Log.e(TAG, "Failed to create new JoggingEntries DB");
		}
	}
    
    public void close() {
        db.close();
    }

    public void addJogEntry(Jog entry) {
        ContentValues initialValues = new ContentValues();
	        initialValues.put(JogsDBSQL.JOG_NAME, entry.getName());
	        initialValues.put(JogsDBSQL.JOG_START_TIME, DBHelperTools.expectedFormat(entry.getStartTime()));
	        initialValues.put(JogsDBSQL.JOG_STOP_TIME, DBHelperTools.expectedFormat(entry.getStopTime()));
	        db.insert(JogsDBSQL.DATABASE_TABLE1, null, initialValues);
    }
    
    public void deleteJogEntry(long Id) {
        db.delete(JogsDBSQL.DATABASE_TABLE1, "id=" + Id, null);
    }

    public List<Jog> fetchAllJogEntries(){
        ArrayList<Jog> ret = new ArrayList<Jog>();
        Cursor c = db.query(JogsDBSQL.DATABASE_TABLE1, JogsDBSQL.COLUMNS_IN_JOG_ENTRY, null, null, null, null, null);
        
        int numRows = c.count();
        c.first();
        for (int i = 0; i < numRows; ++i) {
            ret.add(new Jog(c.getLong(0), c.getString(1), c.getLong(2), c.getLong(3)));
            c.next();
        }
        
        return ret;
    }
    
    public List<Jog> fetchAllTodaysJogEntries(){
	    ArrayList<Jog> ret = new ArrayList<Jog>();
		Cursor c =  db.query(
				JogsDBSQL.DATABASE_TABLE1,
				JogsDBSQL.COLUMNS_IN_JOG_ENTRY,
				JogsDBSQL.WHERE_ENTRIES_WERE_TODAY,
				null, null, null, null);
		
	    int numRows = c.count();
	    c.first();
	    for (int i = 0; i < numRows; ++i) {
	    	
	        ret.add(new Jog(c.getLong(0), c.getString(1), DBHelperTools.expectedFormat(c.getString(2)), DBHelperTools.expectedFormat(c.getString(3))));
	        c.next();
	    }
	    
	    return ret;
	}

	public List<Jog> fetchAllJogEntries(Integer day, Integer month, Integer year) {
	    ArrayList<Jog> ret = new ArrayList<Jog>();
	    String WHERE_SPECIFIED_DATE = JogsDBSQL.getWhereCondition(day, month, year);
	    
	    Cursor c = db.query(JogsDBSQL.DATABASE_TABLE1, JogsDBSQL.COLUMNS_IN_JOG_ENTRY, WHERE_SPECIFIED_DATE, null, null, null, null);
	    
	    int numRows = c.count();
	    c.first();
	    for (int i = 0; i < numRows; ++i) {
	    	ret.add(new Jog(c.getLong(0), c.getString(1), DBHelperTools.expectedFormat(c.getString(2)), DBHelperTools.expectedFormat(c.getString(3))));
	        c.next();
	    }
	    
	    return ret;
	}

	public List<Jog> fetchAllThisMonthsJogEntries(Calendar calendar){
        ArrayList<Jog> ret = new ArrayList<Jog>();
        
		Cursor c =  db.query(
				JogsDBSQL.DATABASE_TABLE1,
				JogsDBSQL.COLUMNS_IN_JOG_ENTRY,
				JogsDBSQL.whereEntriesWereThisMonth(calendar),
				null, null, null, null);
		
        int numRows = c.count();
        c.first();
        for (int i = 0; i < numRows; ++i) {
        	Long jogId = c.getLong(0);
        	String jogName = c.getString(1);
        	String startTime = c.getString(2);
        	String stopTime = c.getString(3);
        	
            ret.add(new Jog(jogId, jogName, DBHelperTools.expectedFormat(startTime), DBHelperTools.expectedFormat(stopTime)));
            c.next();
        }
        
        return ret;
    }
    
    public Jog fetchJogEntry(long Id) {
        Jog row = new Jog();
        Cursor c =
        db.query(true, JogsDBSQL.DATABASE_TABLE1, JogsDBSQL.COLUMNS_IN_JOG_ENTRY, "Id=" + Id, null, null, null, null);
        
        if (c.count() > 0) {
            c.first();
            row.set_id(c.getLong(0));
            row.setName(c.getString(1));
            row.setStartTime(c.getLong(2));
            row.setStopTime(c.getLong(3));
            
            return row;
        } else {
            row.set_id(-1);
            row.setName(null);
        }
        return row;
    }

    public void updateJogEntry(long Id, Jog entry) {
        ContentValues args = new ContentValues();
        args.put(JogsDBSQL.JOG_ID, entry.get_id());
        args.put(JogsDBSQL.JOG_NAME, entry.getName());
        args.put(JogsDBSQL.JOG_START_TIME, entry.getStartTime());
        args.put(JogsDBSQL.JOG_STOP_TIME, entry.getStopTime());
        
        db.update(JogsDBSQL.DATABASE_TABLE1, args, "id=" + Id, null);
    }

	public Cursor getCursorOfAllJogs() {
		return db.query(JogsDBSQL.DATABASE_TABLE1, JogsDBSQL.COLUMNS_IN_JOG_ENTRY, null, null, null, null, null);
	}

}



